let mysqlpool = require("../../model/connect");
var schedule = require('node-schedule');
var moment = require('moment');
const post_order=async (request,response)=>{
    // console.log(request.body);
    let charge_id=request.body.charge_id; 
    let person=request.body.person; 
    let buyTime=request.body.buyTime; 
    let start_time=request.body.start_time; 
    let finishTime=request.body.finishTime; 
    let type=request.body.type; 
    let xuhao = request.body.xuhao;

    let sql_insert
    let sql_check = `select id from order_form order by id desc limit 0,1`;
    try {
        //修改订单数据库
        await mysqlpool.query(sql_check, async (err,res)=>{
            let order_id = res[0].id;
            order_id = ((order_id+1).toString()).padStart(3,'0')
            // console.log(order_id);
            //充电类型没有start_time
            if(!start_time) sql_insert = `insert into order_form (charge_id,person,type,buyTime,finishTime,order_id) values ("${charge_id}","${person}","${type}","${buyTime}","${finishTime}","${order_id}")`;
            else sql_insert = `insert into order_form (charge_id,person,type,buyTime,start_time,finishTime,order_id) values ("${charge_id}","${person}","${type}","${buyTime}","${start_time}","${finishTime}","${order_id}")`;
            await mysqlpool.query(sql_insert, async (err,res)=>{
                if(res.affectedRows){
                    //充电完成后充电桩状态变回来
                    
                    let year = moment(finishTime).year();
                    //程序里的1月份是0
                    let month = moment(finishTime).month();
                    let date = moment(finishTime).date();
                    let hour = moment(finishTime).hour();
                    let minute = moment(finishTime).minute();
                    
                    var date1 = new Date(year,month,date,hour,minute,30);
                    //到了结束时间修改数据库状态
                    var j = schedule.scheduleJob(date1, async function(){
                        sql_check = `select charge_station_use,charge_station_status from charge_station_info WHERE charge_id = ${charge_id}`
                        await mysqlpool.query(sql_check, async (err,res)=>{
                            let use = res[0].charge_station_use;
                            let status = res[0].charge_station_status;
                            let new_status = status.substring(0, xuhao) + `0` + status.substring(xuhao+1, use.length)
                            //取出表单charge_station和charge_station_info信息
                            let sql_change = `UPDATE charge_station_info SET charge_station_status = '${new_status}',charge_station_use = '${use-1}' WHERE charge_id = '${charge_id}'`
                            await mysqlpool.query(sql_change, (err,res)=>{
                                if(res.affectedRows) console.log("充电时间到修改状态成功");
                                else console.log("充电时间到修改状态失败");
                            }) 
                        })
                    });
                   
                    response.send('ok'); 
                }
            })
        })
    } catch (error) {
        response.send('error');
    }
    
}
module.exports=post_order